<?php

namespace app\controllers;
use app\models\Category;
use Yii;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use \yii\db\Query;

class CategoryController extends Controller
{
    public $enableCsrfValidation = false;
     public function actionIndex()
     {
         $model = new Category();


         $rows = (new \yii\db\Query())
             ->select(['cateId', 'cateName'])
             ->from('category')
//             ->where(['cateId' => '2'])
             ->limit(10)
             ->all();


         return $this->render('index', [
             'model' => $rows,
         ]);
     }
     public function actionAdd()
     {
         if ($this->request->isPost) {
             $postParams=Yii::$app->request->post('Category');
             $m=new Category();
             $m->cateName=$postParams['cateName'];
             $m->img=$postParams['img'];
             $m->createTime=time();

//             var_dump($postParams);
             $m->save();
             var_dump($m->getErrors());exit;

         }
         return $this->render('_form',
         [
             'model'=>new Category(),
             ]);

     }
//    public function actionView($cateId)
//    {
//        return $this->render('view', [
//            $this->module->id => $cateId,
//        ]);
//    }


     public function actionUpdate()
     {
         $id = Yii::$app->request->get('id');
         $result = Category::find()->where(['cateId' => $id])->asArray()->one();

         if ($this->request->isPost) {

             $cateName = Yii::$app->request->post('cateName');
             $img= Yii::$app->request->post('img');

             Category::updateAll(['cateName' => $cateName, 'img' => $img], ['cateId' => $id]);
             return $this->redirect(['category/index']);
         }
         return $this->render('update', [
             'model' => $result,
         ]);
     }
    public function actionDel()
    {
        $id = Yii::$app->request->get('id');
        Category::deleteAll(['cateId' => $id]);
        $this->redirect(['category/index']);
    }
}